package com.example.Controller;

import com.example.Service.impl.UserService;
import com.example.mapper.UserMapper;
import com.example.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;

@RestController
@RequestMapping("/api/auth")
@CrossOrigin
public class AuthController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/register")
    public Result register(@RequestBody Map<String, String> params) {
        String username = params.get("username");
        String phone = params.get("phone");
        String password = params.get("password");
        String confirmPassword = params.get("confirmPassword");
        return userService.register(username, phone, password, confirmPassword);
    }

    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        String password = params.get("password");
        return userService.loginByPhone(phone, password);
    }

    @PostMapping("/change-password")
    public Result changePassword(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        String oldPassword = params.get("oldPassword");
        String newPassword = params.get("newPassword");
        return userService.changePasswordByPhone(phone, oldPassword, newPassword);
    }

    // 忘记密码
    @PostMapping("/forget-password")
    public Result forgetPassword(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        String newPassword = params.get("newPassword");
        String verificationCode = params.get("verificationCode"); // 新增验证码参数获取
        String verificationId = params.get("verificationId"); // 新增验证码标识参数获取

        if (phone == null || newPassword == null || verificationCode == null || verificationId == null) {
            return Result.error("参数缺失");
        }

        // 验证验证码是否正确
        // 从Redis中获取存储的验证码
        String storedCode = redisTemplate.opsForValue().get("SMS_" + verificationId);
        if (storedCode == null || !storedCode.equals(verificationCode)) {
            return Result.error("验证码错误");
        }

        return userService.forgetPassword(phone, newPassword);
    }

    @GetMapping("/username-by-phone")
    public String getUsernameByPhone(@RequestParam String phone) {
        return userService.getUsernameByPhone(phone);
    }

    @PostMapping("/update-username")
    public Result updateUsername(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        String newUsername = params.get("newUsername");
        return userService.updateUsernameByPhone(phone, newUsername);
    }

    @PostMapping("/delete-user")
    public Result deleteUser(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        return userService.deleteUser(phone);
    }

    @GetMapping("/avatar")
    public Result getUser_pic(@RequestParam String phone) {
        try {
            String user_pic = userMapper.selectUser_picByPhone(phone);
            if (user_pic != null && !user_pic.isEmpty()) {
                return Result.success(user_pic);
            }
            return Result.error("未找到头像信息");
        } catch (Exception e) {
            return Result.error("服务端异常");
        }
    }


}